"use strict";
define(["commonUI","getTime", "echarts","global","photoSwiper"], function (
    commonUI, getTime, echarts,global, photoSwiper
) {
    var project = null;
    var user = null;
    var app = {
        init: function () {
            if(!global.existCurrProject()){
                return;
            }
            project = global.getCurrProject();
            user = global.getLoginUser();
            this.initText();
            this.initMap();
            this.initEquipmentContent();
            this.initPhoto();
            this.eventBind();
            // getTime.resize();
            getTime.getNowDate();
            getTime.getTimeInfo();
        },
        /**
        * 渲染文字内容
        */
        initText: function() {
            //用户名显示
            $(".name").text(user.userName)
            //页面右边项目名
            $(".title").text(project.name)
            //项目详情文字
            $(".text-info").text(project.description)
        },
        /**
        * 渲染项目略图
        */
        initPhoto: function() {
            $(".photoSwiper-demo1").attr("href",global.baseImageUrl + project.panel);
            $(".photoSwiper-demo1 img").attr("src",global.baseImageUrl + project.panel);
            $(".photoSwiper-demo1").photoSwiper({
                isReduce: false,
                isEnLarge: false,
                isBackOrigin: false,
                isFind: false,
                isTrackSource: false,
                isFollow: false
            })
        },
        /**
         * 渲染设备内容界面
         */
        initEquipmentContent:function(){
            global.get("/web/main/equipmentContent/"+project.id,function(response){
                $(".bottom-info").empty();
                var equipmentContent = response.data.equipmentContent;
                equipmentContent.forEach(function(item){
                    if(item.num>0){
                        var _item = '<div class="info-wrap"><div class="title-info"><span class="num">'+item.num+'</span><span class="name">'+item.typeName+'</span></div>';
                        
                            _item += '<div class="label-wrap">'
                            item.equipments.forEach(function(device){
                                _item += ' <span>'+device.name+'</span>'
                            })
                            _item += '</div>'
                        
                        _item += '</div>'
                        $(".bottom-info").append(_item);
                    }
                })
            })
        },
        /**
        * 渲染地图
        */
        initMap:function() {
            // var data = [{
            //     name:"location",
            //     value: 1005,
            //     title:project.city,
            // }];

            // var data2 = [{
            //     name:"江苏省",
            //     value: 1005,
            //     title:"江苏省",
            // }];

            // var data3 = [{
            //     name:"黄点",
            //     value: 1005,
            //     title:"",
            // }];
            // var geoCoordMap = {
            //     "location": [project.panelX, project.panelY],
            //     "江苏省": [117.1918, 34.25986],
            //     "黄点": [86.1918, 42.25986]
            // };
            
            // var geoCoordMap2 = {
            // };
            // // console.log(geoCoordMap)
            // function convertData(data) {
            //     var res = [];
            //     for (var i = 0; i < data.length; i++) {
            //         var geoCoord = geoCoordMap[data[i].name];
            //         if (geoCoord) {
            //             res.push({
            //                 title:data[i].title,
            //                 name: data[i].name,
            //                 value: geoCoord.concat(data[i].value)
            //             });
            //         }
            //     }
            //     return res;
            // }
            function generateSeries(){
                var series= [{
                    type: 'map',
                    map: 'chinaMap',
                    geoIndex: 1,
                    aspectScale: 0.75, //长宽比
                    showLegendSymbol: false, // 存在legend时显示
                    tooltip:{
                        show:false
                    },
                    label: {
                        normal: {
                            show: false
                        },
                        emphasis: {
                            show: false
                        }
                    } ,
                    roam: false,
                    
                    itemStyle: {
                        normal: {
                            areaColor: '#103249',
                            borderColor: '#02d4fb',
                            borderWidth: 1
                        },
                        emphasis: {
                            areaColor: '#112e40'
                        }
                    },
                }]
                var mapLocations = null;
                global.get("/web/main/mapLocation",function(response){
                    if(response.success){
                        mapLocations = response.data.mapLocations;
                    }else{
                        alert(response.message);
                    }
                },null,null,false);
                mapLocations.forEach(function(mapLocation){
                    // if(mapLocation.type=="企业地址"){
                    //     var item = {
                    //         type: 'scatter',
                    //         coordinateSystem: 'geo',
                    //         tooltip:{
                    //             show:true,
                    //             backgroundColor: 'rgba(0,0,0,0)',
                    //             borderWidth: 0,
                    //             padding: 0,
                    //             formatter: function (params) {
                    //                 return '<div style="position: relative;"><img src="../../images/main_10.png">'+
                    //                 '<span style="position:absolute;top:6px;left:12px;color:#fff;">'+params.name+'</span>'+
                    //                 '<span style="position:absolute;top:32px;left:16px;color:#000;">'+mapLocation.name+'</span></span></div>'
                    //             }
                    //         },
                    //         symbol: 'image://../../images/main_09.png',
                    //         symbolSize: [80, 80],
                    //         symbolOffset: [0, 0],
                    //         z: 99,
                    //         data:[{
                    //             name: mapLocation.content,
                    //             title: mapLocation.name,
                    //             value:[mapLocation.longitude,mapLocation.latitude,1005]
                    //         }],
                    //     }
                    // }else if(mapLocation.type=="项目地址"){
                    //     var item = {
                    //         type: 'scatter',
                    //         coordinateSystem: 'geo',
                    //         tooltip:{
                    //             show:true,
                    //             backgroundColor: 'rgba(0,0,0,0)',
                    //             borderWidth: 0,
                    //             padding: 0,
                    //             formatter: function (params) {
                    //                 var text = "";
                    //                 var str = mapLocation.name;
                    //                 while(str.length>9){
                    //                     text += str.substr(0,9) + "<br>";
                    //                     str = str.substr(9);
                    //                 }
                    //                 text+=str;
                    //                 return '<div style="position: relative;"><img src="../../images/main_13.jpg">'+
                    //                 '<span style="position:absolute;top:16px;left:16px;color: #fff;">'+text+'</span></div>'
                    //             }
                    //         },
                    //         itemStyle: {
                    //             color: '#feff5b',
                    //         },
                    //         symbolSize: [10, 10],
                    //         symbolOffset: [0, 0],
                    //         z: 999,
                            
                    //         data:[{
                    //             name: mapLocation.content,
                    //             title: mapLocation.name,
                    //             value:[mapLocation.longitude,mapLocation.latitude,1005]
                    //         }],
                    //     }
                    // }
                    if(mapLocation.type=="企业地址"){
                        // var item = {
                        //     type: 'scatter',
                        //     coordinateSystem: 'geo',
                        //     tooltip:{
                        //         show:true,
                        //         backgroundColor: 'rgba(0,0,0,0)',
                        //         borderWidth: 0,
                        //         padding: 0,
                        //         formatter: function (params) {
                        //             return '<div style="position: relative;"><img src="../../images/main_10.png">'+
                        //             '<span style="position:absolute;top:6px;left:12px;color:#fff;">'+params.name+'</span>'+
                        //             '<span style="position:absolute;top:32px;left:16px;color:#000;">'+mapLocation.name+'</span></span></div>'
                        //         }
                        //     },
                        //     symbol: 'image://../../images/main_09.png',
                        //     symbolSize: [80, 80],
                        //     symbolOffset: [0, 0],
                        //     z: 99,
                        //     data:[{
                        //         name: mapLocation.content,
                        //         title: mapLocation.name,
                        //         value:[mapLocation.longitude,mapLocation.latitude,1005]
                        //     }],
                        // }
                    }else if(mapLocation.type=="项目地址"){
                        if(mapLocation.name!=project.name){
                            var item = {
                                type: 'scatter',
                                coordinateSystem: 'geo',
                                tooltip:{
                                    show:true,
                                    backgroundColor: 'rgba(0,0,0,0)',
                                    borderWidth: 0,
                                    padding: 0,
                                    formatter: function (params) {
                                        var text = "";
                                        var str = mapLocation.name;
                                        while(str.length>9){
                                            text += str.substr(0,9) + "<br>";
                                            str = str.substr(9);
                                        }
                                        text+=str;
                                        return '<div style="position: relative;"><img src="../../images/main_13.jpg">'+
                                        '<span style="position:absolute;top:16px;left:16px;color: #fff;">'+text+'</span></div>'
                                    }
                                },
                                itemStyle: {
                                    color: '#feff5b',
                                },
                                symbolSize: [10, 10],
                                symbolOffset: [0, 0],
                                z: 999,
                                
                                data:[{
                                    name: mapLocation.content,
                                    title: mapLocation.name,
                                    value:[mapLocation.longitude,mapLocation.latitude,1005]
                                }],
                            }
                        }else{
                            var item = {
                                type: 'scatter',
                                coordinateSystem: 'geo',
                                tooltip:{
                                    show:true,
                                    backgroundColor: 'rgba(0,0,0,0)',
                                    borderWidth: 0,
                                    padding: 0,
                                    formatter: function (params) {
                                        return '<div style="position: relative;"><img src="../../images/main_10.png">'+
                                        '<span style="position:absolute;top:6px;left:12px;color:#fff;">'+params.name+'</span>'+
                                        '<span style="position:absolute;top:32px;left:16px;color:#000;">'+mapLocation.name+'</span></span></div>'
                                    }
                                },
                                symbol: 'image://../../images/main_09.png',
                                symbolSize: [80, 80],
                                symbolOffset: [0, 0],
                                z: 99,
                                data:[{
                                    name: mapLocation.content,
                                    title: mapLocation.name,
                                    value:[mapLocation.longitude,mapLocation.latitude,1005]
                                }],
                            }
                        }
                    }
                    series.push(item);
                })
                return series;
            }
            var mapChart = echarts.init(document.getElementById("mapWrap"));
            $.get("/js/common/china.json", function (areaInfo) {
                echarts.registerMap("chinaMap", areaInfo);
                var mapOption = {
                geo: {
                    show: true,
                    map: 'chinaMap',
                    label: {
                        emphasis: {
                            show: true,
                            color: '#fff'
                        }
                    },
                    roam: false,
                    itemStyle: {
                        normal: {
                            areaColor: '#103249',
                            borderWidth: 1,//设置外层边框
                            borderColor:'#02d4fb',
                            shadowColor: '#02d4fb',
                            shadowBlur: 30
                        },
                        emphasis:{
                            areaColor: '#112e40',
                        }
                    }
                },
                tooltip: { 
                    trigger: 'item',
                },
                series: generateSeries()
                
                }
                mapChart.setOption(mapOption);
            });
        },
        /** 事件绑定 */
        eventBind: function () {
            $(document).on("click", ".toDetail", function () {
                Dialog.open({
                    title: "查看详情", 
                    theme: "detailDialog",
                    width: 730, 
                    height: 504,
                    context: self,
                    // content: "<div class='info-text'>某某经济技术开发区能源站，建筑面积951.25㎡,为半地下室结构，地下2.85米，地上2.7米，建筑层高5.5米，开发区能源站分期建设，一期主要为开发区金融大厦供暖，根据本工程建设的具体情况及地理位，某某经济技术开发区能源站，建筑面积951.25㎡,为半地下室结构，地下2.85米，地上2.7米，建筑层高5.5米，开发区能源站分期建设，一期主要为开发区金融大厦供暖，根据本工程建设的具体情况及地理位，某某经济技术开发区能源站，建筑面积951.25㎡,为半地下室结构，地下2.85米，地上2.7米，建筑层高5.5米，开发区能源站分期建设，一期主要为开发区金融大厦供暖，根据本工程建设的具体情况及地理位置。</div>",
                    content: "<div class='info-text'>"+project.description+"</div>",
                    button: [
                        {id:"mybtn1", label:"关闭", intent:"primary"}
                    ]
                });
            })
            window.onresize = function () {
                // getTime.resize();
            };
        },
        
    };
    return app;
});